import torch
import torch.nn.functional as F
from torch import nn
from torch import inf

# 假设 y_pred 是模型的预测输出，shape 为 [batch_size, num_classes]
# y 是真实的标签，shape 为 [batch_size]
criterion = nn.CrossEntropyLoss()

for i in range(100):
    y_pred = torch.randn(3, 5)*10
    # y_pred = torch.Tensor([
    #     [0.0,1.0,0.0,0.0,0.0],
    #     [0.0,0.0,1.0,0.0,0.0],
    #     [0.0,0.0,0.0,1.0,0.0]
    # ])
    # y_pred = torch.Tensor([
    #     [-inf,1.0,-inf,-inf,-inf],
    #     [-inf,-inf,1.0,-inf,-inf],
    #     [-inf,-inf,-inf,1.0,-inf]
    # ])
    y = torch.tensor([1, 2, 3])
    loss = F.nll_loss(y_pred, y)
    # print(loss)
    loss1 = criterion(y_pred,y)
    print(loss1)

"""
nll_loss  是计算0，1之间范围的量 ，当相应正确的类别的值是1，不正确类别的值是0 的时候，则达到loss最小值 -1
当输入为0-1之间的值的时候，可以使用 nllloss,也可使用CrossEntropyLoss
CrossEntropyLoss  计算的是正负无穷的量，只有当其他类别都是-inf时，loss才可以达到最小值0.
当输入范围扩大到其他范围，则最好使用也可使用CrossEntropyLoss
"""
